//
//  DDMenuView.m
//  Dingdong
//
//  Created by 程强 on 16/11/30.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDMenuView.h"
#import "DDHomeCategorySet.h"

@interface DDMenuView()
@property (strong, nonatomic) UIScrollView *menuScroll;
@property (strong, nonatomic) NSArray *bgColorAry;
@end

@implementation DDMenuView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:scrollView];
        self.menuScroll = scrollView;
        self.bgColorAry = @[
                            RGBColor(208,174,240),
                            RGBColor(236,163,217),
                            RGBColor(173,183,235),
                            RGBColor(173,235,217),
                            RGBColor(238,175,192),
                            RGBColor(208,174,240),
                            RGBColor(173,235,217),
                            RGBColor(236,163,217)];
    }
    return self;
}
- (void)setMenus:(NSArray *)menus
{
    _menus = menus;
    
    CGFloat width = 135;
    CGFloat height = 60;
    
    for (int i = 0; i<menus.count; i++) {
        DDHomeCategoryObj *obj = menus[i];
        DDMenuViewItem *btn = [[DDMenuViewItem alloc]initWithFrame:CGRectMake(15+(width+10)*i, 10, width, height)];
        if (i>=self.bgColorAry.count) {
            btn.bgColor =self.bgColorAry[i-self.bgColorAry.count];
        }else{
            btn.bgColor =self.bgColorAry[i];
        }
        btn.itemName =obj.title;
        btn.icon = obj.atturl;
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(menuClick:) forControlEvents:UIControlEventTouchUpInside];
        [_menuScroll addSubview:btn];
    }
    [_menuScroll setContentSize:CGSizeMake(15+(width+10)*menus.count+5, self.bounds.size.height)];
}
- (void)menuClick:(UIButton *)sender
{
    NSInteger index = sender.tag-100;
    if (self.delegate && [self.delegate respondsToSelector:@selector(menu:didSelectMenuIndex:)]) {
        [self.delegate menu:self didSelectMenuIndex:index];
    }
}
@end

@interface DDMenuViewItem()
@property (strong, nonatomic) UIImageView *btnImage;
@property (strong, nonatomic) KKLabel *nameLbl;
@end

@implementation DDMenuViewItem
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _btnImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self addSubview:_btnImage];
        [_btnImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@25);
            make.height.equalTo(@25);
            make.left.equalTo(self).offset(10);
            make.centerY.equalTo(self).offset(5);
        }];
        
        _nameLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",[UIColor whiteColor],[UIFont systemFontOfSize:13.0],NSTextAlignmentLeft);
        }];
        _nameLbl.numberOfLines = 0;
        [self addSubview:_nameLbl];
        [_nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset(-5);
            make.left.equalTo(_btnImage.mas_right).offset(5);
            make.right.equalTo(self).offset(-5);
            make.height.equalTo(@35);
        }];
    }
    return self;
}
- (void)setBgColor:(UIColor *)bgColor
{
    _bgColor = bgColor;
    self.backgroundColor = bgColor;
}
- (void)setIcon:(NSString *)icon
{
    _icon = icon;
    [_btnImage sd_setImageWithURL:[NSURL URLWithString:icon] placeholderImage:nil];
}
- (void)setItemName:(NSString *)itemName
{
    _itemName = itemName;
    self.nameLbl.text = itemName;
}
@end
